package com.adayo.memoryfree.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.adayo.memoryfree.R;
import com.adayo.memoryfree.base.AbsListAdapter;
import com.adayo.memoryfree.bean.CacheListItem;

import java.util.ArrayList;
import java.util.List;

public class RublishMemoryAdapter extends AbsListAdapter<CacheListItem> implements AdapterView.OnItemClickListener {

	public static List<Integer> clearIds;

	public RublishMemoryAdapter(Context context, List<CacheListItem> apps) {
		super(context, apps);
		clearIds = new ArrayList<>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.listview_rublish_clean,
					parent, false);
			holder = new ViewHolder();
			holder.appIcon = (ImageView) convertView
					.findViewById(R.id.app_icon);
			holder.appName = (TextView) convertView
					.findViewById(R.id.app_name);
			holder.size = (TextView) convertView
					.findViewById(R.id.app_size);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final CacheListItem item = (CacheListItem) getItem(position);
		if (item != null) {
			holder.appIcon.setImageDrawable(item.getApplicationIcon());
			holder.appName.setText(item.getApplicationName());
			holder.size.setText(Formatter.formatShortFileSize(context, item.getCacheSize()));
			holder.packageName = item.getPackageName();
		}
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();

		if (viewHolder != null && viewHolder.packageName != null) {
			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			intent.setData(Uri.parse("package:" + viewHolder.packageName));

			context.startActivity(intent);
		}
	}

	class ViewHolder {
		ImageView appIcon;
		TextView appName;
		TextView size;
		String packageName;
	}
}
